# -*- coding: utf-8 -*-
from inode import *
from log import *
from simple import *
from stat import *
from fuse import *

from sys import *

def lala(a, b):
	return b(a)

def b(a):
	return a + 1
	
def nachin(*args):
	for arg in args:
		print arg

class Test:
	a = 0
	b = 0

if __name__ == '__main__':
	inode = Inode()
	print "Data:", inode.data
	inode.data = 'caaaaaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrne'
	print "Data:", inode.data
	print "Metadata:", inode.metadata.st_atime
	
	log = Log('lala', 3)
	log.debug1("lasdk %s", 123)
	log.debug2("lasdk %s", 123)
	log.debug3("lasdk %s", 123)
	
	#sp = Simple()
	#sp.simple_mknod('/', 12312, 12)
	#sp.simple_mknod('/josealberto', 1234445, 1)
	#print sp
	#inode_attr = sp.simple_getattr('/josealberto')
	#print inode_attr
	#inode_attr = sp.simple_getattr('/')
	#print inode_attr
	a = 0
	print lala(a, b)
	
	lalala = '/aksjdlkasj/jshwjw22222/1111111111/'
	lalala1 = '/aksjdlkasj'
	
	print (lalala[1:]).partition('/')
	print (lalala1[1:]).partition('/')
	
	if (S_ISLNK(S_IFLNK)):
		 print 'wu'
		 
	print Stat()
	
	nachin(argv)
	
	
	
	
	def unlink (self, path):
		new_time = gmtime(0)
		father = path.rpartition('/')[0]
		if father != '':
			inode_father = self.__table[father]
			inode_father.set_metadata(CONST.ST_MTIME, new_time)
			inode_father.set_metadata(CONST.ST_CTIME, new_time)

		inode = self.__table[path]
		current_nlink = inode.set_metadata(CONST.ST_NLINK)
		inode.set_metadata(CONST.ST_NLINK, current_nlink - 1)

		del self.__table[path]
		if inode.get_metadata[CONST.ST_NLINK == 0]:
			inode.set_metadata(CONST.ST_CLINK, new_time)
		return 0

	def rename (self, current_path, new_path):
		new_time = gmtime(0)

		inode = self.__table[current_path]
		del self.__table.pop[current_path]
		self.__table[new_path] = inode
		inode.set_metadata(CONST.ST_MTIME, new_time)
		inode.set_metadata(CONST.ST_CTIME, new_time)
		inode.set_metadata(CONST.ST_ATIME, new_time)

		return 0

	def chmod(self, path, mode):
		new_time = gmtime(0)
		inode = self.__table[path]
		inode.set_metadata(CONST.ST_MODE, mode)
		inode.set_metadata(CONST.ST_CTIME, new_time)

		return 0

	def chown(self, path, uid, gid):
		new_time = gmtime(0)
		inode = self.__table[path]

		if (uid >= 0) :# Porque lo castea a entero en C?
			inode.set_metadata(CONST.ST_UID, uid)

		if (gid >= 0) :# Porque lo castea a entero en C?
			inode.set_metadata(CONST.ST_GID, gid)
		inode.set_metadata(CONST.ST_CTIME, new_time)

		return 0

	def truncate (self, path, new_size):
		new_time = gmtime(0)
		inode = self.__table[path]
		current_size = inode.get_metadata(CONST.ST_SIZE)

		if (new_size != current_size):
			data = inode.get_data()
			data = data[0:new_size]# Revisar bien los casos limites.

			inode.set_metadata(CONST.ST_SIZE, new_size)

			inode.set_metadata(CONST.ST_MTIME, new_time)
			inode.set_metadata(CONST.ST_CTIME, new_time)
			inode.set_metadata(CONST.ST_ATIME, new_time)

		return 0

	def read (self, path, size, offset):
		new_time = gmtime(0)
		inode = self.__table[path]

		st_size = inode.get_metadata(CONST.ST_SIZE)
		data = inode.get_data()

		if (offset < st_size):
			if ((size + offset) > st_size):
				size = st_size - offset;

			assert (size + offset <= st_size), \
			'Invalid size. Error trying to read with size:%s y offset: %s' % (size, offset)
			buff = data[offset:size]
			inode.set_metadata(CONST.ST_ATIME, new_time)

		else:
			size = 0;

		return size, buff

	def write (self, path, buff, size, offset):
		new_time = gmtime(0)
		inode = self.__table[path]

		st_size = inode.get_metadata(CONST.ST_SIZE)
		data = inode.get_data()

		inodo.set_metadata(CONST.ST_SIZE, size + offset)
		st_size = inode.get_metadata(CONST.ST_SIZE)
		assert (size + offset <= st_size), \
		'Invalid size. Error trying to read with size:%s y offset: %s' % (size, offset)

		data = data + buff
		inode.set_data(data)

		inode.set_metadata(CONST.ST_MTIME, new_time)
		inode.set_metadata(CONST.ST_ATIME, new_time)

		return size

	def utimens (self, path, timespec): # Esta funcion no la entendi :P :P
		new_time = gmtime(0)
		inode = self.__table[path]

		inode.set_metadata(CONST.ST_MTIME, new_time)
		inode.set_metadata(CONST.ST_CTIME, new_time)
		inode.set_metadata(CONST.ST_ATIME, new_time)

		inode.set_metadata(CONST.ST_MTIME, timespec[1])
		inode.set_metadata(CONST.ST_ATIME, timespec[2])

		return 0

	def readlink (self, path, size):
		inode = self.__table[path]

		st_mode = inode.get_metadata(CONST.ST_MODE)
		if (S_ISLNK(st_mode)):
			buf = inode.get_data()
			return buf

		else:
			return -EXDEV


	def symlink(self, target, name):
		assert (target != None and name != None), 'Invalid path. %s or %s, are incorrect' % (target, name)

		simple_mknod(self, name, S_IFLNK, None)# Curioso caso de pasar rdev con None
		inode = self.__table[name]
		inode.set_data(target)

		return 0

	def link(self, target, name):
		assert (target != None and name != None), 'Invalid path. %s or %s, are incorrect' % (target, name)

		new_time = gmtime(0)
		inode = self.__table[target]
		st_nlink = inode.get_metadata(CONST.ST_NLINK)
		inode.set_metadata(CONST.ST_NLINK, st_nlink + 1)
		inode.set_metadata(CONST.ST_ATIME, new_time)

		self.__table[name] = inode

		return 0

	def mkdir(self, path, mode):
		assert (path != None), 'Invalid path; %s' % path

		if (self.__table.has_key(path)):
			simple_mknod(self, path, mode | S_IFDIR)
			return 0
		else:
			return EEXIST

	def rmdir(self, path):
		assert (path != None), 'Invalid path; %s' % path

		new_time = gmtime(0)
		extended_path = path + '/'
		father = path.rpartition('/')[0]

		if(not(father == '/')):
			inode_father = self.__table[father]
			inode_father.set_metadata(CONST.ST_CTIME, new_time)
			inode_father.set_metadata(CONST.ST_MTIME, new_time)

		key_list = self.__table.keys()

		for key in key_list:
			if((key == path) | (key == extended_path)):
				count += 1

		if(count == 1):
			self.__table.pop(path)

		else:
			return -ENOTEMPTY

		return 0
